<!-- 站点编辑弹窗 -->
<template>
  <el-dialog :destroy-on-close="false" :lock-scroll="false" :title="isUpdate?'修改类型':'添加类型'" :visible="visible"
             width="750px" @update:visible="updateVisible">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="类型编码：">
        <span v-if="!isUpdate">自动生成</span>
        <span v-else>{{ form.type_number }}</span>
      </el-form-item>
      <el-form-item label="设备型号：" prop="name">
        <el-input v-model="form.name" placeholder="请输入设备型号"/>
      </el-form-item>
      <el-form-item label="产品分类：" prop="category_name">
        <el-autocomplete v-model="category_name" :fetch-suggestions="querySearchType" placeholder="请搜索类型名称"
                         value-key="name" @select="handleSelectType"/>
      </el-form-item>
      <el-form-item label="类型照片：" >
        <uploadImage v-model="form.image" :limit="1"/>
      </el-form-item>
      <el-form-item label="类型简介：" >
        <el-input v-model="form.intro" :rows="3" placeholder="请输入类型简介" type="textarea" maxlength="200" show-word-limit/>
      </el-form-item>
      <el-form-item label="质保开始时间：" prop="warranty_start_type">
        <el-select v-model="form.warranty_start_type" placeholder="请选择质保开始时间">
          <el-option v-for="item in start_types" :key="item.code" :label="item.title" :value="item.code"/>
        </el-select>
      </el-form-item>
      <el-form-item label="质保时长：" prop="warranty_cycle_count">
        <el-input v-model="form.warranty_cycle_count" placeholder="请输入质保时长" style="margin-right: 20px;" type="number"/>

        <el-select v-model="form.warranty_start_unit" placeholder="年/月" style="width: 40%;"
                   >
          <el-option v-for="item in start_units" :key="item.code" :label="item.title" :value="item.code"/>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="save">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage.vue'

export default {
  name: 'TypeEdit',
  components: {uploadImage},
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    const checkcCtegory_name = (rule, value, callback) => {
      if (!this.category_name) {
        return callback(new Error('请选择产品分类'));
      }else {
        callback()
      }
    };  const checkcWarranty = (rule, value, callback) => {
      if (!this.form.warranty_cycle_count) {
        return callback(new Error('请输入质保时长'));
      }else if(this.form.warranty_cycle_count&&!this.form.warranty_start_unit){
        return callback(new Error('请选择质保单位'));
      }else {
         callback()
      }
    };
    return {
      // 表单数据
      form: Object.assign({status: 1}, this.data),
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      start_types: [],
      start_units: [],
      category_name: "",
      rules: {
        name: [
          {required: true, message: "请输入设备型号", trigger: "blur"}
        ],
        category_name: [
          {validator: checkcCtegory_name, trigger: 'change' ,required:true}
        ],
        category_id: [
          {required: true, message: "请搜索类型名称", trigger: "blur"}
        ],
          // image: [
          //   {required: true, message: "请上传类型照片", trigger: "blur"}
          // ],
          // intro: [
          //   {required: true, message: "请输入类型简介", trigger: "blur"}
          // ],
        warranty_start_type: [
          {required: true, message: "请选择质保开始时间", trigger: "change"}
        ],
        warranty_cycle_count: [
          {validator: checkcWarranty, trigger: 'change',required:true}
        ]
      }
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.loading = true
        this.$http.get(`/product_types/${this.data.id}`).then(res => {
          if (res.data.code === 0) {
            this.form = Object.assign({warranty_start_unit: 'year'}, res.data.data);
            this.category_name = res.data.data.category_name
            this.form.article_ids = this.form.articles.map(item => item.id)
          } else {
            this.$message.error(res.data.msg);
          }
          this.loading = false;
        })

        // this.category_name = this.data.category.name;
        // this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.category_name = "";
        this.form = {};
        this.isUpdate = false;
      }
    }
  }
  ,
  mounted() {
    this.productParams();
  }
  ,
  methods: {
    // 获取商品类型参数
    productParams() {
      this.$http.get('product_types/params').then(res => {
        if (res.data.code === 0) {
          this.start_types = res.data.data.start_types;
          this.start_units = res.data.data.start_units;
        }
      }).catch(e => {
        this.$refs.BrandEdit.loading = false;
        this.$message.error(e.message);
      });
    }
    ,
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          // 编辑
          if (this.isUpdate) {
            this.$emit("editType", this.form, this.form.id);
          } else {
            // 新增
            this.$emit("addType", this.form);
          }
          // this.category_name=''
          // this.$refs.form.resetFields()
        } else {
          return false;
        }
      });
    }
    ,
    cancel() {
      this.updateVisible(false);
      this.category_name = "";
      this.$refs.form.resetFields();

      this.isUpdate = false;
    }
    ,
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
    ,
    querySearchType(query, cb) {
      this.$http.get(`categories?limit=10&name=${query}`, ).then((res) => {
        cb(res.data.data);
      });
    }
    ,
    handleSelectType(item) {
      this.form.category_id = item.id;
    }
    ,
  }
}
</script>

<style scoped>
/deep/ .el-form-item__content {
  display: flex;
}

.el-select {
  width: 100%;
}

.el-autocomplete {
  width: 100%;
}
</style>
